package bufferings.lab.basic.check;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Logger;

import org.junit.Test;
import org.slim3.datastore.Datastore;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;

public class BatchPutLimit {

  private static final Logger logger =
    Logger.getLogger(BatchPutLimit.class.getName());

  @Test
  public void put100() {
    put(100);
  }

  @Test
  public void put200() {
    put(200);
  }

  @Test
  public void put300() {
    put(300);
  }

  @Test
  public void put320() {
    put(320);
  }

  @Test
  public void put322() {
    put(322);
  }

  @Test
  public void put324() {
    put(324);
  }

  @Test
  public void put326() {
    put(326);
  }

  @Test
  public void put328() {
    put(328);
  }

  @Test
  public void put330() {
    put(330);
  }

  @Test
  public void put332() {
    put(332);
  }

  @Test
  public void put334() {
    put(334);
  }

  @Test
  public void put336() {
    put(336);
  }

  @Test
  public void put338() {
    put(338);
  }

  @Test
  public void put340() {
    put(340);
  }

  @Test
  public void put360() {
    put(360);
  }

  @Test
  public void put380() {
    put(380);
  }

  @Test
  public void put400() {
    put(400);
  }

  @Test
  public void put500() {
    put(500);
  }

  private void put(int count) {
    logger.info("put entities count=" + count);

    DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
    List<Entity> list = new ArrayList<Entity>();
    Iterator<Key> keys = ds.allocateIds("Bar", count).iterator();

    for (int i = 0; i < count; i++) {
      Entity entity = new Entity(keys.next());
      list.add(entity);
    }

    logger.info("before put");
    ds.put(list);
    logger.info("after put");
  }

  @Test
  public void clear() {
    logger.info("before query");
    List<Key> keys = Datastore.query("Bar").asKeyList();
    logger.info("after query");
    Datastore.delete(keys);
    logger.info("after delete");
  }

}
